<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 菜單管理
 *@param datetime 2012-01-06
 *@param author solgh
 ********************/
class Logoimg extends Admins_Controller {
  
  private $data=array();

  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='manages';
    $this->load->model('logoimg_mdl');
	$this->data['lang'] =$this->lang->language;
    $this->get_categorys(); //獲取logo
    $this->get_position();
  }

  public function index(){
    $this->get_list();
    $this->load->view('admins/logo_list_view',$this->data);
  }
    //獲取頁面列表
    private function get_list(){
        $page = $this->input->get('p',TRUE);
        $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
        $limit = $this->settings['default_admin_limit']; //後臺列表默認個數.
        $offset = ($page - 1) * $limit;
        if($offset < 0){
            redirect('admins/logoimg');
        }
        $url_query=$_SERVER['QUERY_STRING'];
        if(!empty($url_query)){
            $url_query=preg_replace('/&p=(\d+)/','',$url_query);
        }
        $this->data['category']=$this->input->get('category');
        $this->data['position']=$this->input->get('position');

        $this->data['field']=$this->input->get('field');
        $this->data['sort']=$this->input->get('sort');
        $this->data['field']=!empty($this->data['field'])?$this->data['field']:'sort_order';
        $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'asc';

        $data=array('limits'=>$limit,'offset'=>$offset);
        $data=!empty($this->data['category'])?array_merge($data,array('title'=>$this->data['category'])):$data;
        $data=!empty($this->data['position'])?array_merge($data,array('position'=>$this->data['position'])):$data;

        $data=!empty($this->data['field'])?array_merge($data,array('field'=>$this->data['field'])):$data;
        $data=!empty($this->data['sort'])?array_merge($data,array('sort'=>strtoupper($this->data['sort']))):$data;

        $this->data['lists_count']=$this->logoimg_mdl->totle_logoimg($data);

        $pagination = '';
        if($this->data['lists_count'] > $limit)
        {
            $this->dpagination->currentPage($page);
            $this->dpagination->items($this->data['lists_count']);
            $this->dpagination->limit($limit);
            $this->dpagination->adjacents(5);
            $this->dpagination->target(site_url('admins/logoimg').'?'.$url_query);
            $this->dpagination->parameterName('p');
            $this->dpagination->nextLabel($this->data['lang']['orders_lable_19']);
            $this->dpagination->PrevLabel($this->data['lang']['orders_lable_18']);
            $pagination = $this->dpagination->getOutput();
        }
        $this->data['pagination'] = $pagination;
        $this->data['lists']=$this->logoimg_mdl->get_logoimg($data);
    }
  
  //添加廣告
  public function add(){
    $this->load->view('admins/logo_form_view',$this->data);
  }
    //修改廣告
  public function edit($logoimg_id=''){
     if(!empty($logoimg_id) && intval($logoimg_id)>0){
       
         $result=$this->logoimg_mdl->get_logoimg_by_id($logoimg_id);
         $this->data['edit_id']=$logoimg_id;
         $this->data['logo']=$result['logo'];
         $this->data['url']=$result['url'];
         $this->data['title']=$result['title'];
         $this->data['category']=$result['category'];
         $this->data['position']=$result['position'];
         $this->data['sort_order']=$result['sort_order'];
         $this->data['status']=$result['status'];
         $this->load->view('admins/logo_form_view',$this->data);
     }else{
       show_error('對不起,您的參數出錯!!!');
       exit;
     }
  }
  //執行修改或添加
  public function modify(){
    $edit_id=$this->input->post('edit_id');
    $this->_load_validation_rules();
    if ($this->form_validation->run() == FALSE){
      $this->data['error']=true;
      $this->load->view('admins/logo_form_view',$this->data);
    }else{
      $data=array(
        'logo'=>$this->input->post('logo'),
        'url'=>$this->input->post('url'),
        'title'=>$this->input->post('title'),
        'category'=>$this->input->post('category'),
        'position'=>$this->input->post('position'),
        'status'=>$this->input->post('status'),
        'sort_order'=>$this->input->post('sort_order')
      );
       $result=0;
        if(!empty($edit_id) && $edit_id>0){ //修改
          $operator_title='修改首頁圖標->'.$data['logo'];
          $action='修改';
          $result=$this->logoimg_mdl->edit_logoimg($data,$edit_id);
        }else{ //添加
          $operator_title='新增首頁圖標->'.$data['logo'];
          $action='新增';
          $data=array_merge($data,array('add_time'=>time()));
          $result=$this->logoimg_mdl->add_logoimg($data);
        }
        $this->operator_log($operator_title,$action,$result);
        $msg = ($result > 0)?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
        $notify = ($result > 0)?'success':'error';
        $this->nsession->set_flashdata($notify, $msg); 
        redirect('admins/logoimg');
    }

  }
  //驗證廣告添加
  private function _load_validation_rules(){
    $this->form_validation->set_rules('logo','菜單圖標','trim|required|max_length[100]');
    $this->form_validation->set_rules('title','描述','trim|max_length[100]');
    $this->form_validation->set_rules('sort_order','排序','trim|numeric');
    $this->form_validation->set_rules('edit_id','修改ID','trim|numeric');
    $this->form_validation->set_rules('status','狀態','trim|required|numeric|max_length[1]');
  }
   /**
     +----------------------------------------------------------
     *  獲取類別中文描述
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     */
  public function get_categorys($key=''){
       $data=array(
          'home'=>'主頁',
        );
      if(!empty($key)){
        return $data[$key];
      }else{
        $this->data['ccc']=$data;
        return $this->data['ccc'];
      }
  }

  public function get_position($key=''){
       $data=array(
        'logo'=>'菜單圖標',
        );
      if(!empty($key)){
        return $data[$key];
      }else{
        $this->data['positions']=$data;
        return $this->data['positions'];
      }
  }

    
  //刪除圖片.
  public function delete($delete_id=0)
  {
    if(!empty($delete_id)){
      $result=$this->logoimg_mdl->delete_logoimg($delete_id);
      $this->operator_log('刪除圖標->ID:'.$delete_id,'刪除',$result);
      $msg = ($result > 0)?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
      $notify = ($result > 0)?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      redirect("admins/logoimg");
    }else{
      show_error('對不起,您的參數出錯!');
      exit;
    }
  }
   //批量刪除
  public function delete_batch(){
     $delete_string=$this->input->get('delete_string');
     if(!empty($delete_string)){
        $del_arr=explode(',',$delete_string);
        $result=$this->logoimg_mdl->delete_batch($del_arr);
        $this->operator_log('批量刪除圖標->ID:'.$delete_string,'批量刪除',$result);
        $msg = ($result > 0)?$this->data['lang']['global_lable_48']:$this->data['lang']['global_lable_49'];
        $notify = $result > 0?'success':'error';
        $this->nsession->set_flashdata($notify, $msg);
        redirect("admins/logoimg");
      }else{
        show_error('對不起,參數出錯');
        exit;
      }
  }
}
